package com.sotb.sender;

import android.app.AlertDialog;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.telephony.SmsManager;
import android.widget.Toast;

/**
 * 
 * @author lgb
 * 获取位置信息并发送信息和录音的service
 *
 */
public class SMSSenderService extends Service{
	AlertDialog d;
	private double [] latlng = new double[2];
	boolean isRecord = false;
	boolean isExperience = true;
	LocationInfo locationInfo = null;
	SharedPreferences set = null;
	Thread smsThread = null;
	Thread recordThread = null;
		
	Handler smsHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub		
			// 获取信息内容
			latlng = (double[]) msg.obj;
			String latlngStr = latlng[0] + "," + latlng[1];
			//发信息
			SmsManager sms = SmsManager.getDefault();
			try{
				sms.sendTextMessage(Constant.TARGET_MOBILE_NUM, null, latlngStr, null, null);
				Toast.makeText(SMSSenderService.this, R.string.send_success, Toast.LENGTH_SHORT).show();
			}catch(IllegalArgumentException e){
				e.printStackTrace();
			}
			super.handleMessage(msg);
		}
		
	};
	
	Handler recordHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			//开始录音
			System.out.println("start record");
			Recorder.startRecord();
			super.handleMessage(msg);
		}
		
	};
	
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub		
		set = getSharedPreferences(Constant.PREFERENCE_NAME, MODE_PRIVATE);		
		locationInfo = new LocationInfo(this);
		super.onCreate();
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		//读取设置
		isRecord = set.getBoolean(Constant.IS_RECORD, false);
		isExperience = set.getBoolean(Constant.IS_EXPERIENCE, true);
		if (!isExperience){
			//开启监听线程：
			smsThread = new Thread(new SMSRunnable());
			smsThread.start();
			//如果录音设置为真,则开启录音线程进行录音
			if (isRecord){
				//开启录音线程
				if (recordThread == null){
					recordThread = new Thread(new RecordRunnable());
					recordThread.start();
				}
			}
		}else{
			Toast.makeText(SMSSenderService.this, "Experience Mode", Toast.LENGTH_SHORT).show();
			//震动反馈未写
		}
		return super.onStartCommand(intent, flags, startId);
	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		System.out.println("stop_send_sms");
		//停止发送短信线程
		if (smsThread != null){
			//停止发短信
			locationInfo.stopListening();
			smsThread.stop();
		}
		//如果录音则停止录音线程
		if (recordThread != null){
			//关掉录音
			System.out.println("stop_record");
			Recorder.stopRecord();
			recordThread.stop();
		}
		super.onDestroy();
	}

	class SMSRunnable implements Runnable{

		//@Override
		public void run() {			
			// TODO Auto-generated method stub
			Looper.prepare();
			//非体验模式下开启监听位置			
			locationInfo.startListening(smsHandler, Constant.TOTAL_LISTENING_TIME);	//一定时间之后传回message到handler										
			Looper.loop();
		}
    }
	
	class RecordRunnable implements Runnable{

		//@Override
		public void run() {
			// TODO Auto-generated method stub
			// 发个信息给Handler,通知他开始录音
			Looper.prepare();
			Message msg = recordHandler.obtainMessage();
			msg.sendToTarget();
			Looper.loop();
		}		
	}
		
}
